package com.iqiyi.pps.epg.core.service.basement;

import com.iqiyi.pps.epg.core.dao.base.Page;
import com.iqiyi.pps.epg.core.dao.base.PropertyFilter;
import com.iqiyi.pps.epg.core.dao.basement.ConstantsMapDao;
import com.iqiyi.pps.epg.core.model.basement.ConstantsMap;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: fangying
 * Date: 12-8-27
 * Time: 下午5:05
 * To change this template use File | Settings | File Templates.
 */
@Component
@Transactional
public class ConstantsMapManager implements ConstantsMapService {
    @Autowired
    private ConstantsMapDao constantsMapDao;

    @Override
    public List<ConstantsMap> getList(Integer type) {
        return constantsMapDao.findBy("type",type);
    }

    @Override
    public void save(ConstantsMap constantsMap) {
        constantsMapDao.save(constantsMap);
    }

    @Override
    public ConstantsMap get(Long id) {
        return constantsMapDao.get(id);
    }

    @Override
    public Page<ConstantsMap> getConstantsMapList(Page<ConstantsMap> page, List<PropertyFilter> filters) {
        return constantsMapDao.getConstantsMapList(page,filters);
    }

    /**
     * 验证自字段唯一性
     *
     * @param type
     * @param old
     * @param newValue
     * @param type
     * @return
     */
    @Override
    public boolean isPropertityUnique(String checkType, String old, String newValue,Integer type) {
        if(old!=null&&old.equals(newValue)){
            return true;
        }
        Criterion criteriaType=  Restrictions.eq("type", type);
        Criterion criteriaCheckType=  Restrictions.eq(checkType, newValue);
        ConstantsMap constantsMap=constantsMapDao.findUnique(criteriaType,criteriaCheckType);
        return constantsMap==null;
    }

    @Override
    public ConstantsMap get(Integer type, String code) {
        return constantsMapDao.get(type,code);  //To change body of implemented methods use File | Settings | File Templates.
    }

}
